﻿'
' Bring2mind - http://www.bring2mind.net
' Copyright (c) 2011
' by Bring2mind
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
'

Imports Bring2mind.DNN.Modules.Signup.Business
Imports DotNetNuke.Common
Imports DotNetNuke.Security.Membership
Imports DotNetNuke.Entities.Users
Imports DotNetNuke.Services.Localization.Localization

Public Class SignupConfirm
 Inherits ModuleBase

 Private _magicKey As String = ""
 Private _signup As SignupInfo = Nothing

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

  _magicKey = Request.Params("key")
  _signup = SignupsController.GetSignupByKey(PortalId, _magicKey)

  If _signup Is Nothing Then
   pnlNewUser.Visible = False
   pnlError.Visible = True
   lblError.Text = String.Format(GetString("KeyNotFound.Error", LocalResourceFile), _magicKey)
   Exit Sub
  End If

  If Not Me.IsPostBack Then

   chkNewsLetter.Checked = True
   If Settings.NewsletterRoleId < 0 Then
    chkNewsLetter.Visible = False
    lblNewsLetter.Visible = False
   End If

   Dim objUser As UserInfo = FindUserByEmail(_signup.Email)
   If objUser IsNot Nothing Then
    pnlNewUser.Visible = False
    pnlError.Visible = True
    lblError.Text = String.Format(GetString("Registered.Error", LocalResourceFile), objUser.Username)
   End If

   valPasswordComplexity.ValidationExpression = Settings.PasswordRegex

   ' Maintenance
   SignupsController.CleanUpOldSignups(PortalId, Now.AddDays(-7))

  End If

 End Sub

 Private Sub cmdConfirm_Click(sender As Object, e As System.EventArgs) Handles cmdConfirm.Click

  Dim objUser As UserInfo = FindUserByEmail(_signup.Email)
  If objUser IsNot Nothing Then
   Exit Sub
  End If

  ' Create and hydrate User
  objUser = New DotNetNuke.Entities.Users.UserInfo
  objUser.Profile.InitialiseProfile(Me.PortalId, True)
  objUser.PortalID = Me.PortalId
  objUser.DisplayName = String.Format("{0} {1}", txtFirstName.Text, txtLastName.Text).Trim
  objUser.Email = _signup.Email
  objUser.FirstName = txtFirstName.Text
  objUser.LastName = txtLastName.Text
  objUser.Username = _signup.Email
  objUser.Membership.CreatedDate = Date.Now
  objUser.Membership.Password = txtPassword.Text
  objUser.Membership.UpdatePassword = True
  objUser.Membership.Approved = True

  ' Create the user
  Dim objAnswer As UserCreateStatus = UserController.CreateUser(objUser)

  Select Case objAnswer
   Case UserCreateStatus.Success
    ' Connecte l'utilisateur
    If objUser.Membership.Approved Then
     Dim strHostName As String = System.Net.Dns.GetHostName()
     Dim IP As String = System.Net.Dns.GetHostAddresses(strHostName).GetValue(0).ToString()
     DotNetNuke.Entities.Users.UserController.UserLogin(Me.PortalId, objUser.Username, objUser.Membership.Password, "", Me.PortalSettings.PortalName, IP, UserLoginStatus.LOGIN_SUCCESS, False)
    End If
   Case Else
    pnlNewUser.Visible = False
    pnlError.Visible = True
    lblError.Text = String.Format(GetString("Registration.Error", LocalResourceFile), objAnswer.ToString)
    Exit Sub
  End Select

  '    'Send Notification to User
  Select Case PortalSettings.UserRegistration
   Case PortalRegistrationType.NoRegistration
    ' Do nothing
   Case PortalRegistrationType.PrivateRegistration
    DotNetNuke.Services.Mail.Mail.SendMail(objUser, DotNetNuke.Services.Mail.MessageType.UserRegistrationPrivate, Me.PortalSettings)
   Case PortalRegistrationType.PublicRegistration
    DotNetNuke.Services.Mail.Mail.SendMail(objUser, DotNetNuke.Services.Mail.MessageType.UserRegistrationPublic, Me.PortalSettings)
   Case PortalRegistrationType.VerifiedRegistration
    DotNetNuke.Services.Mail.Mail.SendMail(objUser, DotNetNuke.Services.Mail.MessageType.UserRegistrationVerified, Me.PortalSettings)
  End Select

  If chkNewsLetter.Checked And Settings.NewsletterRoleId > -1 Then
   Dim objRoles As New DotNetNuke.Security.Roles.RoleController
   Dim urc As New DotNetNuke.Security.Roles.RoleController
   If urc.GetUserRole(PortalId, objUser.UserID, Settings.NewsletterRoleId) Is Nothing Then
    urc.AddUserRole(PortalId, objUser.UserID, Settings.NewsletterRoleId, DotNetNuke.Common.Utilities.Null.NullDate)
    End If
  End If

  SignupsController.DeleteSignup(_signup.SignupID)

  If Settings.RedirectAfterConfirmationTabId > -1 Then
   Response.Redirect(DotNetNuke.Common.NavigateURL(Settings.RedirectAfterConfirmationTabId), False)
  Else
   Response.Redirect(DotNetNuke.Common.NavigateURL, False)
  End If

 End Sub

 Private Function FindUserByEmail(email As String) As UserInfo

  Dim users As ArrayList = UserController.GetUsersByEmail(PortalId, email, 1, 1, 1)
  If users.Count > 0 Then
   Return CType(users.Item(0), UserInfo)
  Else
   Return Nothing
  End If

 End Function

 Private Sub cmdReturn_Click(sender As Object, e As System.EventArgs) Handles cmdReturn.Click

  Response.Redirect(DotNetNuke.Common.NavigateURL, False)

 End Sub
End Class